// pages/personalInfo/personalInfo.js
import request from '../../../utils/request'

Page({
  data: {
    user: null,
    userInfo: {
      // 初始化数据
      avatar: '',
      name: '',
      employeeNumber: '',
      department: '',
      positionName: '',
      phone: '',
      email: '',
      hireDate: '',
      type: '',
      status: '',
      statusText: '',
      gender: '',
      personalPayment: 0,
      companyPayment: 0,
      totalPayment: 0,
      paymentMonth: '',
      contractId: ''
    },
    loading: true
  },

  onLoad() {
    const app = getApp();
    const employeeId = app.getUserInfo().id;
    if (employeeId) {
      this.fetchUserInfo(employeeId)
      console.log(employeeId,"222222222222222222")
    } else {
      wx.showToast({
        title: '未获取到员工信息',
        icon: 'none'
      })
      
    }
  },

  // 获取用户信息
  async fetchUserInfo(id) {
    try {
      const res = await request.get(`/zdtj/zdtj/${id}`)
      if (res.code === 200) {
        this.setData({
          userInfo: this.formatUserInfo(res.data),
          loading: false
        })
      } else {
        wx.showToast({
          title: res.msg || '获取信息失败',
          icon: 'none'
        })
      }
    } catch (error) {
      console.error('获取用户信息失败:', error)
      wx.showToast({
        title: '获取信息失败',
        icon: 'none'
      })
    }
  },

  // 格式化用户信息
  formatUserInfo(data) {
    // 部门映射
    const departmentMap = {
      104: '技术部',
      105: '产品部',
      106: '设计部'
    }
    
    // 状态映射
    const statusMap = {
      formal: '正式',
      leave: '离职',
      probation: '试用期'
    }
    
    // 性别映射
    const genderMap = {
      male: '男',
      female: '女'
    }
    
    return {
      ...data,
      department: departmentMap[data.departmentId] || '未知部门',
      statusText: statusMap[data.status] || data.status,
      gender: genderMap[data.gender] || data.gender,
      hireDate: this.formatDate(data.hireDate)
    }
  },

  // 格式化日期
  formatDate(dateStr) {
    if (!dateStr) return '未知'
    const date = new Date(dateStr)
    return `${date.getFullYear()}年${(date.getMonth() + 1).toString().padStart(2, '0')}月${date.getDate().toString().padStart(2, '0')}日`
  },

  // 拨打电话
  makePhoneCall(e) {
    const phone = e.currentTarget.dataset.phone
    if (!phone) {
      wx.showToast({
        title: '无有效电话号码',
        icon: 'none'
      })
      return
    }
    
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: () => {
        wx.showToast({
          title: '拨号失败',
          icon: 'none'
        })
      }
    })
  },

  // 复制邮箱
  copyEmail() {
    if (!this.data.userInfo.email) {
      wx.showToast({
        title: '无有效邮箱地址',
        icon: 'none'
      })
      return
    }
    
    wx.setClipboardData({
      data: this.data.userInfo.email,
      success: () => {
        wx.showToast({
          title: '邮箱已复制',
          icon: 'success'
        })
      }
    })
  },

  // 查看合同
  viewContract() {
    if (!this.data.userInfo.contractId) {
      wx.showToast({
        title: '无合同信息',
        icon: 'none'
      })
      return
    }
    
    wx.navigateTo({
      url: `/pages/contractDetail/contractDetail?id=${this.data.userInfo.contractId}`
    })
  }
})